﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class PageImageInstance : UserControl
    {
        private PageImage m_pageImage = new PageImage();
        private WidgetServiceProxy m_service;

        public PageImageInstance()
        {
            InitializeComponent();
            PanelController.Visibility = Visibility.Collapsed;
        }

        private void Refresh()
        {
            TxtUrl.Text = m_pageImage.ImageUrl;
            try
            {
                string url = m_pageImage.ImageUrl;
                if (url.Contains(@"http://") == false)
                    url = WidgetHelper.MapFileUrl(
                        OperationManager.Instance.CurrentSpace.OwnerUserID, url);
                ImgMain.Source = new BitmapImage(new Uri(url));
            }
            catch
            {
                ImgMain.Source = new BitmapImage();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            m_service = OperationManager.Instance.CreateWidgetServiceProxy(this);
            m_service.GetSessionCompleted += (OnGetSession);
            m_service.GetSession("image");
        }

        void OnGetSession(object sender, DragDraw.UserService.GetSessionCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Result))// no session at all
                return;
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(PageImage));
                XmlReader xr = XmlReader.Create(new StringReader(e.Result));
                m_pageImage = (PageImage)xs.Deserialize(xr);
            }
            catch
            {
                m_pageImage.ImageUrl = "";
            }
            Refresh();
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            m_pageImage.ImageUrl = TxtUrl.Text;
            StringWriter sw = new StringWriter();
            XmlSerializer xs = new XmlSerializer(typeof(PageImage));
            xs.Serialize(sw, m_pageImage);
            m_service.SetSession("image", sw.ToString());
            Refresh();
        }

        private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            if (WidgetHelper.IsInMySpace)
            {
                PanelController.Visibility = Visibility.Visible;
            }
        }

        private void LayoutRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            PanelController.Visibility = Visibility.Collapsed;
        }
    }

    public class PageImage
    {
        public string ImageUrl { get; set; }
    }
}
